Pinvon's Blog

所见, 所闻, 所思, 所想

模板

Table of Contents

实例

template<class T> class String {
    struct Srep;
    Srep *rep;
public:
    String();
    String(const T*);
    String(const String&);
    T read(int i) const;
    // ...
};

在使用的时候, 可以这么写:

String<char> cs;
String<unsigned char> us;
String<wchar_t> ws;
class Jchar{
    // ...
};
String<Jchar> js;

在标准库中, 提供了模板类 basic_string, 常使用的 basic_string<char> 由于太长, 不方便书写, 因此使用 typedef 将其定义为 string 的同义词, 同时可以隐藏细节:

typedef basic_string<char> string;

词频统计:

int main(){
    string buf;
    map<string, int> m;
    while(cin >> buf) m[buf]++;
    // ...
}

定义一个模板类

在上面的实例中, 已经看到了如何定义一个模板类, 模板类的成员在类外的定义方法如下:

template<class T> struct String<T>::Srep{
    T* s;  // 到元素的指针
    int sz;  // 元素个数
    int n;  // 引用计数
    // ...
};

// 先写 template<class T>
// 再写返回类型 T
// 定义类成员函数: String<T>::read(int) const
template<class T> T String<T>::read(int) const {
    return rep->s[i];
}

template<class T> String<T>::String() {
    rep = new Srep(0, T());
}

// 构造函数的定义也可以写成这样:
template<class T> String<T>::String<T>() {
    rep = new Srep(0, T());
}

Comments

使用 Disqus 评论
comments powered by Disqus